package com.tx.axure.tkaxureweb.controller;

import com.tx.axure.tkaxureweb.annotation.JspPage;
import com.tx.axure.tkaxureweb.annotation.LoginRequired;
import com.tx.axure.tkaxureweb.domain.entity.*;
import com.tx.axure.tkaxureweb.mapper.*;
import com.tx.axure.tkaxureweb.pojo.AccountApply;
import com.tx.axure.tkaxureweb.vo.ResultVoStr;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 主要负责跳转jsp页面
 *
 * @Author ChenZhaoHui
 * @Date 2022/6/26 12:33
 */
@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PageController {
    private final SProdMapper sProdMapper;
    private final SProdTypeMapper sProdTypeMapper;
    private final SProdGroupMapper sProdGroupMapper;
    private final SUserApplyMapper sUserApplyMapper;
    private final SUserMapper sUserMapper;

    @Value("${file.visit}")
    private String visitPath;

    @GetMapping("welcome")
    public String welcome(HttpServletRequest request) {
        request.setAttribute("name", "czh");
        return "index";
    }

    @GetMapping("login")
    public String login(HttpServletRequest request) {
        return "login";
    }

    @GetMapping("regist")
    public String regist(HttpServletRequest request) {
        return "regist";
    }

    /**
     * 注册页面提交
     * @param sUserApply
     * @return
     */
    @PostMapping("regist/submit")
    public @ResponseBody
    ResultVoStr doRegist(SUserApply sUserApply) {

        SUser sUser=new SUser();
        sUser.setUserName(sUserApply.getAccount().trim());
        sUser.setSts(true);
        if(sUserMapper.selectCount(sUser)>0){
            return new ResultVoStr(2001,"您的账号可直接登录");
        }

        SUserApply checkDTO=new SUserApply();
        checkDTO.setAccount(sUserApply.getAccount().trim());
        checkDTO.setSts(true);
        int acNum=sUserApplyMapper.selectCount(checkDTO);
        if(acNum>0){
            return new ResultVoStr(2000,"账号已经申请注册");
        }

        //检测账号是否重复
        sUserApply.setAccount(sUserApply.getAccount().trim());
        sUserApply.setPwd(sUserApply.getPwd().trim());
        sUserApply.setCreateTime(new Date());
        sUserApply.setSts(true);
        sUserApply.setStatus(0);
        sUserApplyMapper.insertSelective(sUserApply);
        return new ResultVoStr();
    }

    /**
     * 注册列表页，显示注册申请列表
     * @param
     * @return
     */
    @JspPage
    @LoginRequired
    @GetMapping("regist/list")
    public String registList(HttpServletRequest request) {
        List<SProdType> prodTypeList = sProdTypeMapper.selectAll();
        request.setAttribute("prodTypeList", prodTypeList);
        SUserApply sUserApply=new SUserApply();
        Example example=Example.builder(SUserApply.class).
                orderByAsc("status").orderByDesc("createTime")
                .build();

        request.setAttribute("registList", sUserApplyMapper.selectByExample(example));
        return "regist_list";
    }

    @PostMapping("regist/confirm")
    @LoginRequired
    public @ResponseBody
    ResultVoStr registConfirm(AccountApply accountApply) {
        SUserApply applyDto=new SUserApply();
        applyDto.setId(accountApply.getId());
        applyDto.setStatus(1);
        applyDto.setUpdateTime(new Date());
        sUserApplyMapper.updateByPrimaryKeySelective(applyDto);
        SUserApply transUserApply=sUserApplyMapper.selectByPrimaryKey(accountApply.getId());

        SUser sUser=new SUser();
        sUser.setUserType(accountApply.getUserType());
        sUser.setUserName(transUserApply.getAccount());
        sUser.setPwd(transUserApply.getPwd());
        sUser.setMobile(transUserApply.getMobile());
        sUser.setSts(true);
        sUser.setNickName(transUserApply.getRealName());
        sUser.setCompanyName(transUserApply.getCompanyName());
        sUser.setUserType(accountApply.getUserType());
        sUser.setCreateTime(new Date());
        sUserMapper.insertSelective(sUser);
        return new ResultVoStr();
    }


    /**
     * 1添加原型 2添加交互稿
     *
     * @param type
     * @return
     */
    @LoginRequired
    @GetMapping("prod/to_create/{type}")
    public String to_create(@PathVariable Integer type, HttpServletRequest request) {
        request.setAttribute("prodType", sProdTypeMapper.selectByPrimaryKey(type));

        List<SProdGroup> prodGroupList = sProdGroupMapper.select(SProdGroup.builder().sts(true).build());
        request.setAttribute("prodGroupList", prodGroupList);
        if (3 == type.intValue()) {
            return "other_create";
        }
        return "create";
    }

    /**
     * 跳转到修改页面
     *
     * @return
     */
    @JspPage
    @LoginRequired
    @GetMapping("prod/to_edit/{prodId}")
    public String to_edit(@PathVariable Integer prodId, HttpServletRequest request) {
        SProd sProdQuery = SProd.builder().id(prodId).build();
        SProd sProd = sProdMapper.selectOne(sProdQuery);
        request.setAttribute("sProd", sProd);
        request.setAttribute("prodType", sProdTypeMapper.selectByPrimaryKey(sProd.getProdType()));
        List<SProdGroup> prodGroupList = sProdGroupMapper.select(SProdGroup.builder().sts(true).build());
        request.setAttribute("prodGroupList", prodGroupList);
        return "edit";
    }

    @PostMapping("create")
    @LoginRequired
    public @ResponseBody
    ResultVoStr doCreate(SProd sProd) {

        sProd.setCreateTime(new Date());
        sProd.setSts(true);
        sProdMapper.insertSelective(sProd);
        return new ResultVoStr();
    }

    @RequestMapping("edit")
    @LoginRequired
    public @ResponseBody
    ResultVoStr doEdit(SProd sProd, HttpServletRequest request) {
        sProdMapper.updateByPrimaryKeySelective(sProd);
        return new ResultVoStr();
    }

    @LoginRequired
    @GetMapping("delete/{id}")
    public @ResponseBody
    ResultVoStr doDelete(@PathVariable Integer id) {
        SProd sProd = SProd.builder()
                .id(id)
                .sts(false)
                .build();
        sProdMapper.updateByPrimaryKeySelective(sProd);
        return new ResultVoStr();
    }

    @GetMapping("create-test")
    @ResponseBody
    public SProd create(HttpServletRequest request) {
        SProd sProd = SProd.builder()
                .prodName("用户中心")
                .hostName("usercenter")
                .welcomeFile("index.html")
                .fileSize("5M")
                .createTime(new Date())
                .sts(true)
                .build();
        sProdMapper.insertSelective(sProd);
        return sProd;
    }

    @JspPage
    @GetMapping("list")
    public ModelAndView list(HttpServletResponse response) throws java.io.IOException {
        response.sendRedirect("/list/1");
        return null;
    }


    @JspPage
    @GetMapping("list/{prodType}")
    public String index(HttpServletRequest request, @PathVariable Integer prodType) {
        List<SProdType> prodTypeList = sProdTypeMapper.selectAll();
        request.setAttribute("prodTypeList", prodTypeList);
        request.setAttribute("type", prodType);
        List<SProd> sProdList = sProdMapper.select(SProd.builder().sts(true).prodType(prodType).build());
        request.setAttribute("sProdList", sProdList);
        request.setAttribute("visitPath", visitPath);
        List prodGroupList = new ArrayList();
        //查询产品分组
        List<SProdGroup> prodGroupListTemp = sProdGroupMapper.select(SProdGroup.builder().sts(true).build());
        for (int i = 0; i < prodGroupListTemp.size(); i++) {
            SProdGroup groupTemp = prodGroupListTemp.get(i);
            SProd sProdTemp = SProd.builder()
                    .sts(true)
                    .prodType(prodType)
                    .groupId(groupTemp.getId()).build();
            int count = sProdMapper.selectCount(sProdTemp);
            if (count > 0) {
                groupTemp.setProdList(sProdMapper.select(sProdTemp));
                prodGroupList.add(groupTemp);
            }
        }
        List nullGroupProdList = sProdMapper.select(SProd.builder().sts(true).groupId(-1).prodType(prodType).build());
        prodGroupList.add(SProdGroup.builder().groupName("其他").id(-1).prodList(nullGroupProdList).build());

        request.setAttribute("prodGroupList", prodGroupList);
        return "list";
    }


    @GetMapping("test/{id}")
    public @ResponseBody
    ResultVoStr test1(@PathVariable Integer id) {
        return new ResultVoStr();
    }

    @GetMapping("health")
    public @ResponseBody
    String health() {
        return "UP";
    }
}
